package library.book;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 书架
 */
public class BookList {
    //书架就是保存书籍的，书本的个数不确定，使用动态数组或者链表
     private List<Book> books=new ArrayList<>();
     //保存当前书架上所有书籍名称，以后的任何操作就是很具书籍这个名称来查找具体的Book对象
     private List<String> booksName=new ArrayList<>();

     public BookList(){
         //产生一个书架时，默认上架四本书
         books.add(new Book("三国演义","罗贯中",66.6,"小说"));
         books.add(new Book("水浒传","施耐庵",55.6,"小说"));
         books.add(new Book("西游记","吴承恩",44.6,"小说"));
         books.add(new Book("红楼梦","曹雪芹",88.6,"小说"));

         //将当前四本书名添加到booksName列表中
         booksName.add("三国演义");
         booksName.add("水浒传");
         booksName.add("西游记");
         booksName.add("红楼梦");
     }

     public void listBooks(){
         for (Book book:books
              ) {
             System.out.println(book);
         }
     }

     public boolean contains(String booksName){
         for (String str: this.booksName
              ) {
             if (str.equals(booksName)){
                 return true;
             }
         }
         return false;
     }

     public void addNewBook(String booksName,String author,String type,double price){
         Book book=new Book(booksName,author,price,type);
         books.add(book);
         this.booksName.add(booksName);
         }

     public boolean isContains(String booksName){
         if(this.booksName.contains(booksName)){
             return true;
         }else {
             return false;
         }
     }

         public void bookUpateOperate(String booksName){
             Book book=findBook(booksName);
             books.remove(book);
             this.booksName.remove(booksName);
         }

     private Book findBook(String booksName){
         Iterator<Book> iterator=books.iterator();
         while(iterator.hasNext()){
             Book book=iterator.next();
             if (book.getName().equals(booksName)){
                 return book;
             }
         }
         return null;
     }

     public boolean isBookBorrow(String booksName){
         Book book=findBook(booksName);
         return book.isBorrroeed();
     }

     public void borrowBook(String booksName){
         Book book=findBook(booksName);
         book.setBorrowed(true);
     }

     public void returnBook(String booksName){
         Book book=findBook(booksName);
         book.setBorrowed(false);
     }

     public void booksUpate(String booksName,String type,double price){
         Book book=findBook(booksName);
         book.setPrice(price);
         book.setType(type);
     }

}
